<?php

namespace app\admin\controller;
use app\admin\model\TripModel;
use app\admin\model\TripCateModel;
use think\Db;

class Trip extends Base
{

    /**
     * [index 旅程列表]
     * @author [魏文峰]
     */
    public function index(){

        $key = input('key');
        $map = [];
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];          
        }       
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 10;// 获取总条数
        $count = Db::name('trip')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $trip = new TripModel();
        $lists = $trip->getTripByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }


    /**
     * [add_trip 添加旅程]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function add_trip()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $trip = new TripModel();
            $flag = $trip->insertTrip($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $cate = new TripCateModel();
        $this->assign('cate',$cate->getAllCate());
        return $this->fetch();
    }


    /**
     * [edit_trip 编辑旅程]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function edit_trip()
    {
        $trip = new TripModel();
        
        if(request()->isAjax()){

            $param = input('post.');         
            $flag = $trip->updateTrip($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $cate = new TripCateModel();
        $this->assign('cate',$cate->getAllCate());
        $this->assign('trip',$trip->getOneTrip($id));
        return $this->fetch();
    }


    /**
     * [edit_trip 编辑旅程]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function edit_trip_price()
    {

        $id = input('param.id');
        $this->assign('id',$id);

        return $this->fetch();
    }


    public function trip_price()
    {

        $id = input('post.id');

        $trip = new TripModel();
        $data = $trip->getOneTripPrice($id);
        $info = $data['price_content'];
        return $info;
    }

    public function add_trip_perice()
    {
        $info = input('post.price_content/a');
        $data['price_content'] = json_encode($info);
        $where['id'] = input('post.id');
        $info = Db::name('trip')->where($where)->setField($data);
        if($info)
        {
            return json_encode(1);
        }else{
            return json_encode(0);
        }

    }

    /**
     * [del_trip 删除旅程]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function del_trip()
    {
        $id = input('param.id');
        $cate = new TripModel();
        $flag = $cate->delTrip($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [trip_state 旅程状态]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function is_recom()
    {
        $id=input('param.id');
//        var_dump($id);
        $status = Db::name('trip')->where(array('id'=>$id))->value('is_recom');//判断当前状态情况
        $recom_count = Db::name('trip')->where('is_recom','1')->count('id');
        if($status==0 )
        {
            if($recom_count>=2){
                return json(['code' => 2,  'msg' => '推荐不能超过两个']);
            }
            $flag = Db::name('trip')->where(array('id'=>$id))->setField(['is_recom'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);

        }
        else
        {
            $flag = Db::name('trip')->where(array('id'=>$id))->setField(['is_recom'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
    
    }



    public function is_jp()
    {
        $id=input('param.id');
//        var_dump($id);
        $status = Db::name('trip')->where(array('id'=>$id))->value('is_jp');//判断当前状态情况
        $recome_count = Db::name('trip')->where('is_jp','1')->count('id');
        if($status==0 )
        {
            if($recome_count>=2){
                return json(['code' => 2,  'msg' => '精品不能超过两个']);
            }
            $flag = Db::name('trip')->where(array('id'=>$id))->setField(['is_jp'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);

        }
        else
        {
            $flag = Db::name('trip')->where(array('id'=>$id))->setField(['is_jp'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }

    }

    //*********************************************分类管理*********************************************//

    /**
     * [index_cate 分类列表]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function index_cate(){

        $cate = new TripCateModel();
        $list = $cate->getAllCate();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * [add_cate 添加分类]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function add_cate()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new TripCateModel();
            $flag = $cate->insertCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $cate = new TripCateModel();
        $list = $cate->getAllCate();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * [edit_cate 编辑分类]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function edit_cate()
    {
        $cate = new TripCateModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $this->assign('cate',$cate->getOneCate($id));
        return $this->fetch();
    }


    /**
     * [del_cate 删除分类]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function del_cate()
    {
        $id = input('param.id');
        $cate = new TripCateModel();
        $flag = $cate->delCate($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [cate_state 分类状态]
     * @return [type] [description]
     * @author [魏文峰]
     */
    public function cate_state()
    {
        $id=input('param.id');
        $status = Db::name('trip_cate')->where(array('id'=>$id))->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('trip_cate')->where(array('id'=>$id))->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('trip_cate')->where(array('id'=>$id))->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }  

}